package com.pet.shop.controller.pet;

import com.pet.shop.controller.BaseController;
import com.pet.shop.entity.option.DataOptionView;
import com.pet.shop.entity.shop.NoRelayDate;
import com.pet.shop.entity.shop.PetRelay;
import com.pet.shop.service.IBaseService;
import com.pet.shop.service.option.IDataOptionService;
import com.pet.shop.service.pet.IPetRelayService;
import com.pet.shop.utils.ApiResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/web/relay")
public class PetRelayController extends BaseController<PetRelay> {

    @Autowired
    private IPetRelayService petRelayService;
    @Autowired
    private IDataOptionService dataOptionService;

    @Override
    public IBaseService<PetRelay> getBaseService() {
        return petRelayService;
    }

    @RequestMapping("list")
    public ModelAndView list(HttpServletRequest request){
        DataOptionView dataOptionViews = dataOptionService.queryDataOptionValByKeyId(-1L, "relayTimes");
        List<DataOptionView> viewList = dataOptionService.queryDataOptionValByTypeId(-1L, 2);
        if(viewList == null) viewList = new ArrayList<>();

        List<NoRelayDate> noRelayDateList = petRelayService.queryOrderByDate(50);
        if(noRelayDateList == null) noRelayDateList = new ArrayList<>();

        return new ModelAndView("pet/relayList").addObject("relayTimes", dataOptionViews)
                .addObject("weekdays", viewList).addObject("noRelayDateList", noRelayDateList);
    }

    @RequestMapping("insertRelayDate")
    @ResponseBody
    public Map<String, Object> insertRelayDate(HttpServletRequest request, String date) {
        if(StringUtils.isEmpty(date)){
            return ApiResultUtil.error_param("日期必选 yyyy-MM-dd");
        }
        Date relayDate = null;
        try {
            relayDate = new SimpleDateFormat("yyyy-MM-dd").parse(date);
        } catch (ParseException e) {
            return ApiResultUtil.error_param("日期格式不对 yyyy-MM-dd");
        };
        NoRelayDate noRelayDate = new NoRelayDate();
        noRelayDate.setCreateTime(new Date());
        noRelayDate.setCreatorId(getSessionUser(request).getId());
        noRelayDate.setRelayDate(relayDate);
        petRelayService.insertNoRelayDate(noRelayDate);
        return ApiResultUtil.success_data(noRelayDate);
    }

    /**
     *
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("deleteNoRelayDate")
    @ResponseBody
    public Map<String, Object> deleteNoRelayDate(HttpServletRequest request, Long id) {
        petRelayService.deleteNoRelayDate(id);
        return ApiResultUtil.success_data("ok");
    }
}
